package LeetCode;

public class Code240_searchMatrix {

    int[][] matrix;

    public boolean searchMatrix(int[][] matrix, int target) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0){
            return false;
        }
        this.matrix = matrix;
        int row = 0;
        int col = matrix[0].length - 1;
        // row和col不越界
        while (isIllegal(row, col)){
            if (matrix[row][col] == target){
                return true;
            } else if (matrix[row][col] < target){
                row++;
            } else {
                col--;
            }
        }
        return false;
    }

    private boolean isIllegal(int row, int col){
        if (row < 0 || row >= matrix.length ||
                col < 0 || col >= matrix[0].length){
            return false;
        }
        return true;
    }
}
